<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" style="margin-top: 20px"  v-show="showSearch" label-width="68px">
        <el-form-item label="项目" prop="projectId">
          <el-select v-model="queryParams.projectId" placeholder="=====请选择项目=====" clearable @change="getList">
            <el-option v-for="pro in projectList" :key="pro.projectId" :label="pro.projectName" :value="pro.projectId">
            </el-option>
          </el-select>
        </el-form-item>
    </el-form>
    <div ref="tu" style="width: 100%; height:800px"></div>
  </div>
</template>

<script>
import {listProjectByUserId} from "@/api/project/project";
import { listProcurementStatistics} from "@/api/prodata/procurementStatistics";
import * as echarts from 'echarts'
export default {
  name: "procurementStatistics",
  data() {
    return {
      loading: true,
      showSearch: true,
      projectList: [],
      myChart: null,
      option: null,
      queryParams:
        {
          projectId: null,
          projectName: null
        },
    }
  },
  mounted() {
    this.myChart = echarts.init(this.$refs.tu);
    this.option = {
      yAxis: {
        type: 'category',
        data: [],
        axisLabel: {
          fontSize: 20
        },
      },
      xAxis: {
        type: 'value',
        min: 0,
        max: 100,
        axisLabel: {
          formatter: '{value}%',
        }
      },
      series: [
        {
          barWidth: 30,
          data: [],
          type: 'bar',
          color: '#3C8EE5',
          label: {
            show: true,
            formatter: function (p) {
              return p.data + '%'
            },
            fontSize: 20,
          }
        }
      ]
    };
    this.myChart.setOption(this.option);
    this.getProjectList();
  },
  methods: {
    getList(){
      this.loading = true;
      listProcurementStatistics({projectId:this.queryParams.projectId}).then(response => {
        this.option.yAxis.data = response.data.nameList;

        this.option.series[0].data = response.data.rateList;
        this.myChart.setOption(this.option);
      });
    },
    getProjectList() {
      listProjectByUserId().then(r => {
        this.projectList = r.data;
        this.queryParams.projectId = this.projectList[0].projectId;
        this.getList();
      })
    }
  },
}
</script>

<style scoped>

</style>
